package cn.bjc.user.controller;

import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.user.domain.User;
import cn.bjc.user.dto.EmailRegisterDto;
import cn.bjc.user.dto.RegisterDto;
import cn.bjc.user.query.UserQuery;
import cn.bjc.user.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private IUserService userService;


    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有")
    public List<User> findAll() {
        return userService.loadAll();
    }


    /**
     * 查询一个:接收参数id
     * url:http://localhost/department/id/
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    public User findOne(@PathVariable("id") long id) {
        return userService.loadById(id);
    }

    /**
     * 删除操作
     *  url:http://localhost/user/id/
     * @param id
     * @return
     * JsonResult:链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            userService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 添加或修改操作
     *
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加操作接口")
    public JsonResult updateOrAdd(@RequestBody User user){
        try {
            if(user.getId()==null){
                //添加操作
                userService.save(user);
            } else {
                userService.update(user);
            }
            return JsonResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     *
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        try {
            return userService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<>();
        }
    }


    /**
     * 批量删除操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            userService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
    
    //用户注册接口-手机号
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            userService.phoneRegister(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试!");
        }

    }


    //用户注册接口-邮箱
    @PostMapping("/emailRegister")
    public JsonResult emailRegister(@RequestBody EmailRegisterDto dto){
        try {
            userService.emailRegister(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试!");
        }

    }
}
